package org.ran.demo.http.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.ran.demo.http.util.FileUtil;
import org.ran.demo.http.util.StreamUtil;

public class MyHttpServer {
    public static void main(String[] args) throws Exception{
        try (ServerSocket serverSocket = new ServerSocket();) {
            serverSocket.bind(new InetSocketAddress(8080));
            Socket socket = serverSocket.accept();
            InputStream in = socket.getInputStream();
            while (true) {
                String request = StreamUtil.readLine(in);
                if (request.isEmpty()) {
                    break;
                }
            }
            OutputStream out = socket.getOutputStream();
            out.write(Files.readAllBytes(Paths.get(FileUtil.RESPONSE_HEADER)));
            out.write(Files.readAllBytes(Paths.get(FileUtil.RESPONSE_BODY)));
            socket.close();
        }
    }
}
